﻿Imports System.Data.SqlClient

Public Class general_settings
    'перехватываем фокус, чтоб колёсико при скроллинге работало
    Private Sub Panel1_mouseenter(ByVal sender As Object, ByVal e As System.EventArgs) Handles Panel1.MouseEnter
        Panel1.Focus()
    End Sub

    Private Sub trusted_CheckedChanged(sender As Object, e As EventArgs) Handles trusted.CheckedChanged
        If trusted.Checked = True Then
            password.Visible = False
            username.Visible = False
            Label3.Visible = False
            Label5.Visible = False
        Else
            password.Visible = True
            username.Visible = True
            Label3.Visible = True
            Label5.Visible = True
        End If
    End Sub

    Private Sub USE_DEFAULT_CheckedChanged(sender As Object, e As EventArgs) Handles usedefaultsettings.CheckStateChanged
        If usedefaultsettings.Checked = True Then
            usedefaultsettings.Checked = True
            usedefaultsettings.Update()

            smptPORT.Enabled = False
            smtpname.Enabled = False
            smtpnamesender.Enabled = False
            smtpsenderemail.Enabled = False
            smtpSSL.Enabled = False
            smtpusername.Enabled = False
            smtpuserpass.Enabled = False

            smptPORT.Text = "587" '587, 25, 465
            smtpname.Text = "smtp.mail.ru"
            smtpnamesender.Text = "Alta2Mail-информатор"
            smtpsenderemail.Text = "alta2mail@mail.ru"
            smtpSSL.Checked = True
            smtpusername.Text = "alta2mail@mail.ru"
            smtpuserpass.Text = "1234"   'пароль никому не скажу :)

        Else
            smptPORT.Enabled = True
            smtpname.Enabled = True
            smtpnamesender.Enabled = True
            smtpsenderemail.Enabled = True
            smtpSSL.Enabled = True
            smtpusername.Enabled = True
            smtpuserpass.Enabled = True

            smptPORT.Text = ""
            smtpname.Text = ""
            smtpnamesender.Text = ""
            smtpsenderemail.Text = ""
            smtpSSL.Checked = False
            smtpusername.Text = ""
            smtpuserpass.Text = ""
        End If
    End Sub

    Private Sub general_settings_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        If Len(My.Settings.SQLpassword) > 0 Then password.Text = start.Pass_Decrypt(My.Settings.SQLpassword)
        If Len(My.Settings.smtppassword) > 0 Then smtpuserpass.Text = start.Pass_Decrypt(My.Settings.smtppassword)
        timersvalue.Text = TrackBar1.Value
        TextBox11.Text = TrackBar2.Value
    End Sub
    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        My.Settings.SQLpassword = start.Pass_crypt(password.Text)
        My.Settings.smtppassword = start.Pass_crypt(smtpuserpass.Text)
        My.Settings.Save()
        start.TopMost = True
        Me.Close()
        start.TopMost = False
    End Sub

    Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
        My.Settings.SQLpassword = start.Pass_crypt(password.Text)
        My.Settings.Save()

        Dim mystringconnection As String
        If My.Settings.SQLtrusted = True Then
            mystringconnection = ("Server =" & My.Settings.nameSQL & ";Database=" & My.Settings.NameDB & ";Trusted_Connection=true;")
        Else
            mystringconnection = ("Server =" & My.Settings.nameSQL & ";Database=" & My.Settings.NameDB & ";User Id=" & My.Settings.SQLusername & "; Password = " & My.Settings.SQLpassword & ";")
        End If
        Dim con As New SqlConnection(mystringconnection)
        Try
            con.Open()
            If (con.State = 1) Then
                start.sysmessage("Соединение с SQL успешно проверено", "", False)
                MsgBox("Соединение успешно установлено", , "Alta2Mail")
            Else
                MsgBox("Ошибка соединения с SQL сервером" & vbNewLine & "Проверьте настройки", , "Alta2Mail")
                start.sysmessage("Ошибка при проверки соединения с SQL", "", False)
                Exit Sub
            End If
            con.Close()
        Catch
            MsgBox("Ошибка при попытке открыть соединение" & vbNewLine & "Проверьте настройки", , "Alta2Mail")
        Finally

        End Try
    End Sub

    Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
        My.Settings.SQLpassword = start.Pass_crypt(password.Text)
        My.Settings.smtppassword = start.Pass_crypt(smtpuserpass.Text)
        My.Settings.Save()
        Me.TopMost = False
        test_send_mail.Show()
    End Sub

    Private Sub Button4_Click(sender As Object, e As EventArgs)
        MsgBox(My.Settings.SQLpassword)
        MsgBox(start.Pass_Decrypt(My.Settings.SQLpassword))
        MsgBox(My.Settings.smtppassword)
        MsgBox(start.Pass_Decrypt(My.Settings.smtppassword))
    End Sub

    Private Sub TrackBar1_Scroll(sender As Object, e As EventArgs) Handles TrackBar1.Scroll, TrackBar2.Scroll
        timersvalue.Text = TrackBar1.Value
        TextBox11.Text = TrackBar2.Value
    End Sub
End Class